package com.wwcom.darteagle.fragment;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.wwcom.darteagle.R;
import com.wwcom.darteagle.data.DartEagleDatabase;
import com.wwcom.darteagle.model.Player;

public class PlayerHistoryPlayerListFragment extends Fragment {
	OnPlayerSelectedListener mListener;
	private ListView playerListView; 
	private PlayerArrayAdapter playerArrayAdapter;

    // Container Activity must implement this interface
    public interface OnPlayerSelectedListener {
        public void onPlayerSelected(Player player);
    }
    
	/** Called when the fragment is first created. */	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {
		     	
        DartEagleDatabase db= new DartEagleDatabase(getActivity());
        List<Player> playerList= db.getPlayers();
        db.close();
        View v= inflater.inflate(R.layout.playerhistory_select_player, container, false); 
	    playerListView= (ListView) v.findViewById(R.id.playerhistory_playerlist);
	    playerArrayAdapter = new PlayerArrayAdapter(getActivity(),playerList);
	    playerListView.setAdapter(playerArrayAdapter);  
	    playerListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				Player player= (Player)(playerListView.getItemAtPosition(pos));	
		        mListener.onPlayerSelected(player);
			}                 
	   });
	    	    
       return v;                 
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnPlayerSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnPlayerSelectedListener");
        }
    }    
        
    
	private class PlayerArrayAdapter extends ArrayAdapter<Player> {
    	private final List<Player> list;
    	private final Context context;

    	public PlayerArrayAdapter(Context context, List<Player> list) {
    		super(context, R.layout.playerhistory_playerlist_textview, list);
    		this.context = context;
    		this.list = list;
    	}

    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		TextView view = null;
    		if (convertView == null) {
    			LayoutInflater inflator = ((Activity) context).getLayoutInflater();
    			view = (TextView)inflator.inflate(R.layout.playerhistory_playerlist_textview, null);    			
    		} else {
    			view = (TextView)convertView;
    		}    		
	    	view.setText(list.get(position).getName());
	    	view.setTag(list.get(position));
    		return view;
    	}
    }
    
}